﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace kehuguanli.Common
{
    public class EncryptString
    {
        public static string MyEncrypt(string str, bool bo)
        {
            char[] cs = str.ToCharArray();
            byte[] by;
            string results = "", temp = "";
            for (int i = 0; i < cs.Length; i++)
            {
                by = System.Text.Encoding.UTF8.GetBytes(cs[i].ToString());
                temp = Convert.ToBase64String(by);//每个字转换为四位
                if (bo)
                {
                    temp += Common.GetMes.Random(10, 99).ToString();//同时加两个随机变量
                }
                results += temp;//（每个变为6位）全部相加
            }
            return results;
        }
        public static string DeMyEnncrypt(string str, int t)
        {
            string result = "", strtemp = "";
            try
            {
                char[] cs = str.ToCharArray();
                string[] temp = Common.GetMes.ArrayListstr(str, t);
                byte[] by;
                for (int i = 0; i < temp.Length; i++)
                {
                    strtemp = (temp[i].ToString()).Substring(0, 4);
                    by = Convert.FromBase64String(strtemp);
                    result += System.Text.Encoding.UTF8.GetString(by);
                }
                return result;
            }
            catch
            {
                return "";
            }
        }
        public static string EncryptMD5(string instr)
        {
            string result;
            try
            {
                byte[] toByte = Encoding.Default.GetBytes(instr);
                MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
                toByte = md5.ComputeHash(toByte);
                result = BitConverter.ToString(toByte).ToLower().Replace("-", "");
            }
            catch
            {
                result = "";
            }
            return result;
        }
    }
}